import {
    MainLogger,
    RendererLogger
} from 'electron-log';
import path from 'path';
import dayjs from 'dayjs';
import AbsolutePlugin from '../AbsolutePlugin';

export default class Logger extends AbsolutePlugin {
    private static mainInstance: MainLogger | null = null;
    private static rendererInstance: RendererLogger | null = null;
    private static logMaxSize: number = 30 * 1024 * 1024;

    constructor(
        private type: 'main' | 'renderer'
    ) {
        super();
        this.initialize(this.type);
    }

    /**
     * 获取日志路径
     * @param type 日志类型
     * @returns 
     */
    private static getLogPath(type: 'main' | 'renderer') {
        return () => {
            const tempBack = process.env.NODE_ENV === 'development' ? '../../' : '../../../';
            const appLogsPath = path.join(__dirname, tempBack, 'logs');
            const day = dayjs().format('YYYY-MM-DD');
            const time = dayjs().format('HH');
            const parentPath = path.join(appLogsPath, type, day);
            let logPath = path.join(parentPath, time + '.log');

            return logPath;
        };
    }

    /**
     * 初始化（单例设计模式）
     * @param type 日志类型
     * @returns 
     */
    public async initialize(type: 'main' | 'renderer') {
        if (type === 'main' && Logger.mainInstance) {
            return;
        } else if (type === 'renderer' && Logger.rendererInstance) {
            return;
        }

        if (type === 'main') {
            Logger.mainInstance = await import('electron-log/main');
            Logger.mainInstance.transports.file.format = '[{y}-{m}-{d} {h}:{i}:{s}] [{level}]{scope} {text}';
            Logger.mainInstance.transports.file.maxSize = Logger.logMaxSize;
            Logger.mainInstance.transports.file.resolvePathFn = Logger.getLogPath('main');
        } else {
            Logger.rendererInstance = await import('electron-log/renderer');
            Logger.rendererInstance.transports.file.format = '[{y}-{m}-{d} {h}:{i}:{s}] [{level}]{scope} {text}';
            Logger.rendererInstance.transports.file.maxSize = Logger.logMaxSize;
            Logger.rendererInstance.transports.file.resolvePathFn = Logger.getLogPath('renderer');
        }
    }

    /**
     * 获取日志实例
     * @returns 
     */
    private getLogger(): MainLogger | RendererLogger | null {
        if (this.type === 'main') {
            return Logger.mainInstance;
        } else {
            return Logger.rendererInstance;
        }
    }

    /**
     * 普通级别日志
     * @param message 日志内容
     */
    info(message: string): void {
        this.getLogger()?.info(message);
    }
    
    /**
     * 警告级日志
     * @param message 日志内容
     */
    warn(message: string): void {
        this.getLogger()?.warn(message);
    }
    
    /**
     * 错误级日志
     * @param message 日志内容
     */
    error(message: string): void {
        this.getLogger()?.error(message);
    }
    
    /**
     * 调试级日志
     * @param message 日志内容
     */ 
    debug(message: string): void {
        this.getLogger()?.debug(message);
    }
}

